package logic

import (
	"context"
	"tiktok/api/internal/svc"
	"tiktok/api/internal/types"
	"tiktok/model/redis"
	"tiktok/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginRequest) (resp *types.ApiResponse, err error) {
	// todo: add your logic here and delete this line
	login, err := l.svcCtx.TiktokSrv.Login(l.ctx, &pb.LoginReq{
		Name:     req.Name,
		Phone:    req.Phone,
		Password: req.Password,
	})
	if err != nil {
		return &types.ApiResponse{
			Code: -1,
			Msg:  "登录失败",
			Data: nil,
		}, nil
	}
	redis.Rdb.Del("send" + "register" + req.Phone)
	return &types.ApiResponse{
		Code: 200,
		Msg:  "登录成功",
		Data: login,
	}, nil
}
